package com.example.rabbitmqdemo;

import com.example.rabbitmqdemo.other.LogMessageHandler;
import com.example.rabbitmqdemo.other.MessagePublisher;
import com.example.rabbitmqdemo.other.MessageSubscriber;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.catalina.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.AbstractJavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author HXB
 * @Title:
 * @Description:
 * @date 2020-04-21 11:29
 */
@RestController
public class TestController {

    @Autowired
    private MsgProducer msgProducer;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private Environment env;

    @Autowired
    private MessagePublisher messagePublisher;

    @Autowired
    private MessageSubscriber messageSubscriber;

    @RequestMapping(value = "/test1")
    public String test1() {
        try {
            messagePublisher.publish("log","123");
            messageSubscriber.subscribe("log",new LogMessageHandler());
        }catch (Exception e){
            return e.getMessage();
        }
        return "ok";
    }

    @RequestMapping(value = "/test")
    public String test() {
        msgProducer.sendMsg("1231321");
        return "ok";
    }
//
//    @RequestMapping(value = "/login", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public String login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
//        try {
//            //TODO：执行登录逻辑
//            User user = new User(userName, password);
//            if (user != null) {//登陆成功
//                //TODO：异步写用户日志
//                try {
//                    UserLog userLog = new UserLog(userName, "Login", "login", objectMapper.writeValueAsString(user));
//                    userLog.setCreateTime(new Date());
//                    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//                    rabbitTemplate.setExchange(env.getProperty("log.user.exchange.name"));
//                    rabbitTemplate.setRoutingKey(env.getProperty("log.user.routing.key.name"));
//
//                    Message message = MessageBuilder.withBody(objectMapper.writeValueAsBytes(userLog)).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
//                    message.getMessageProperties().setHeader(AbstractJavaTypeMapper.DEFAULT_CONTENT_CLASSID_FIELD_NAME, MessageProperties.CONTENT_TYPE_JSON);
//                    rabbitTemplate.convertAndSend(message);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                //TODO：塞权限数据-资源数据-视野数据,其实就是登陆成功后的返回的一些东西
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "response";
//    }
//
//    class UserLog {
//
//        public UserLog(String userName, String login, String login1, String writeValueAsString) {
//        }
//
//        public void setCreateTime(Date date) {
//        }
//    }
//
//    class User {
//        private String userName, password;
//
//        public User(String userName, String password) {
//            this.userName = userName;
//            this.password = password;
//        }
//    }

}
